package erp_services

import (
	"errors"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type stockLogService struct {
}

var StockLogService = new(stockLogService)

func (stockLogService *stockLogService) GetByUnioNo(unionNo string) (stock erp_models.Stock, err error) {
	err = global.App.DB.Where("union_no = ? ", unionNo).Find(&stock).Error

	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

// 新增
func (stockLogService *stockLogService) Create(stockLog erp_models.StockLog) (uint, error) {
	result := global.App.DB.Create(&stockLog)
	if result.Error != nil {
		return 0, result.Error
	}
	return stockLog.ID.ID, nil
}

func (stockLogService *stockLogService) Update(stockLog erp_models.StockLog) (err error) {
	if err := global.App.DB.Model(&erp_models.StockLog{}).Where("ID = ?", stockLog.ID.ID).Omit("ID").Updates(&stockLog).Error; err != nil {
		return err
	}
	return nil
}
